// @flow
import React, { Component } from 'react'
import { TweenLite, TweenMax } from 'gsap'
import styles from './SvgWheel.css'

const arc = 'M250,250 l250,0 A250,250 0 0,0 426.7766952966369,73.22330470336314 z'

const items = [
  {
    title: '总览',
    id: 'overview',
    img: '/wheel/overview.png',
    rotate: 0,
    matrix: '1.100000023841858, 0, 0, 1.100000023841858, -25, -25'
  },
  {
    title: '客户',
    id: 'customer',
    img: '/wheel/customer.png',
    rotate: -45,
    matrix: '0.7071000337600708, -0.7071000337600708, 0.7071000337600708, 0.7071000337600708, -103.5513916015625, 250'
  },
  {
    title: '工况',
    id: 'condition',
    img: '/wheel/condition.png',
    rotate: -90,
    matrix: '6.123233995736766e-17, -1, 1, 6.123233995736766e-17, -2.842170943040401e-14, 500'
  },
  {
    title: '预警',
    id: 'service',
    img: '/wheel/service.png',
    rotate: -135,
    matrix: '-0.7071000337600708, -0.7071000337600708, 0.7071000337600708, -0.7071000337600708, 250, 603.5513916015625'
  },
  {
    title: '故障',
    id: 'malfunction',
    img: '/wheel/malfunction.png',
    rotate: -180,
    matrix: '-1, -1.2246467991473532e-16, 1.2246467991473532e-16, -1, 500, 500'
  },
  {
    title: '履历',
    id: 'resume',
    img: '/wheel/resume.png',
    rotate: -225,
    matrix: '-0.7071000337600708, 0.7071000337600708, -0.7071000337600708, -0.7071000337600708, 603.5513916015625, 250'
  },
  {
    title: 'BOM',
    id: 'bom',
    img: '/wheel/bom.png',
    rotate: -270,
    matrix: '-1.8369701987210297e-16, 1, -1, -1.8369701987210297e-16, 500.00000000000006, 5.684341886080802e-14'
  },
  {
    title: '模型',
    id: 'model',
    img: '/wheel/module.png',
    rotate: -315,
    matrix: '0.7071000337600708, 0.7071000337600708, -0.7071000337600708, 0.7071000337600708, 250, -103.5513916015625'
  }
]

type Props = {
  base: string,
  name: string,
  history: Object,
  imageUrl: string
}
type State = {
  currentId: string
}

export class SvgWheel extends Component {
  props: Props
  state: State
  items: Array<Element>
  handleClick: () => void

  constructor (props: Props) {
    super(props)

    this.state = {
      currentId: 'overview'
    }

    this.handleClick = this.handleClick.bind(this)
  }

  componentWillMount () {
    items.forEach((item) => {
      if (this.props.history.location.pathname.endsWith(item.id)) {
        this.setState({
          currentId: item.id
        })
      }
    })
  }

  componentDidMount () {
    this.wrapText()
    this.items = Array.from(this.refs.svg.querySelectorAll(`.${styles['item']}`))
    TweenLite.set(this.items, {
      scale: 1
    })

    TweenMax.to(this.refs[this.state.currentId], 1, {
      scale: 1.10
    })
  }

  componentDidUpdate (prevProps: Props, prevState: any) {
    this.wrapText()
  }

  wrapText () {
    const textElem: Object = document.querySelector('#trigger-text')
    const maxWidth = 140
    const { name } = this.props
    const textLength = name.length
    const width = textElem.getSubStringLength(0, textLength)
    if (!name || width <= maxWidth) {
      return
    }
    for (let x = textLength; x > 0; x--) {
      if (textElem.getSubStringLength(0, x) <= maxWidth) {
        textElem.textContent
          ? textElem.textContent = name.substring(0, x - 3) + '...'
          : textElem.innerText = name.substring(0, x - 3) + '...'
        return
      }
    }
  }
  shouldComponentUpdate (nextProps: Props) {
    if (nextProps.name !== this.props.name) {
      return true
    }
    return false
  }

  handleClick (item: Object) {
    return (event: Event) => {
      event.preventDefault()

      if (this.state.currentId === item.id) return false

      TweenMax.to(this.items, 0.2, {
        scale: 1
      })

      TweenMax.to(this.refs[item.id], 0.4, {
        scale: 1.10
      })

      this.setState({
        currentId: item.id
      })
      setTimeout(() => {
        this.props.history.push(`${this.props.base}/${item.id}`)
      }, 360)
    }
  }

  render () {
    return (
      <svg xmlns='http://www.w3.org/2000/svg'
        xmlnsXlink='http://www.w3.org/1999/xlink'
        viewBox='-25 -25 554 554' id='menu'
        className='svg'
        ref='svg'
      >
        <g id='symbolsContainer'>
          {
            items.map((item) => (
              <symbol className='icon icon-' id={`icon-${item.id}`} viewBox='0 0 38 58' key={item.id}>
                <image xlinkHref={item.img} x='0' y='0' height='38px' width='38px' />
                <text fill='#222' x='50%' y='80%' dy='.3em' textAnchor='middle' fontSize='1em'>
                  {item.title}
                </text>
              </symbol>
            ))
          }
        </g>
        <g id='itemsContainer'>
          {
            items.map((item) => (
              <a className={styles['item']} id={item.id} role='link' tabIndex='0' ref={item.id}
                transform={`matrix(${item.matrix})`} data-svg-origin='250 250'
                key={item.id}
                onClick={this.handleClick(item)}
              >
                <path fill='none' stroke='#111' strokeWidth='1' className={styles['sector']} d={arc} />
                <use xlinkHref={`#icon-${item.id}`} width='68' height='104'
                  x='377.05950927734375' y='134.94381713867188'
                  transform={`rotate(${-item.rotate} 407.05950927734375 184.94381713867188)`}
                />
                {
                  // transform="rotate(67.5 407.05950927734375 184.94381713867188)"
                }
              </a>
            ))
          }
        </g>
        <g id='trigger' className={`${styles['trigger']} ${styles['menu-trigger']}`} role='button'>
          <circle cx='250' cy='250' r='100' />
          {
            // <image className={styles['wheelImg']} xlinkHref={this.props.imageUrl} x='250' y='250'
            //   height='197px' width='197px' transform='translate(-98.5, -98.5)'
            // />
          }
          <text fill='#222' x='250' y='315' id='trigger-text'
            dy='.3em' width='100px' textAnchor='middle' fontSize='2em'>
            {this.props.name }
          </text>
        </g>
      </svg>
    )
  }
}

export default SvgWheel
